package cn.itcast.web.controller.system;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/system/role", name = "角色管理")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list", name = "查询角色")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        PageInfo page = roleService.findByPage(pageNum, pageSize, getCompanyId());
        request.setAttribute("page", page);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd() {

        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit", name = "新增/修改")
    public String edit(Role role) {
        // 根据id判断
        if (StrUtil.hasEmpty(role.getId())) {
            // 关联企业id和企业名称
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role); // 新增
        } else {
            roleService.update(role);// 修改
        }

        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "修改页面")
    public String toUpdate(String id) {
        // 查询当前角色
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        return "system/role/role-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule", name = "进入模块分配页面")
    public String roleModule(String roleId) {
        Role role = roleService.findById(roleId);
        request.setAttribute("role", role);

        return "system/role/role-module";
    }

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/getZtreeNodes", name = "回显模块树")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleId) {
        List<Map> jsonList = new ArrayList<>();
        // 1.查询出所有的模块列表
        List<Module> moduleList = moduleService.findAll();

        // 2.根据角色id查询中间表，获得模块的id列表
        List<String> moduleIds = roleService.findModuleIdsByRoleId(roleId);

        // 3.将模块列表转为json数组
        for (Module module : moduleList) {
            Map node = new HashMap();
            node.put("id", module.getId());
            node.put("pId", module.getParentId());
            node.put("name", module.getName());
            // 判断如果当前角色选中包含此模块id，增加checked属性
            if (moduleIds.contains(module.getId())) {
                node.put("checked", true);
            }
            jsonList.add(node);
        }

        return jsonList;
    }

    @RequestMapping(value = "/updateRoleModule", name = "实现角色分配权限")
    public String updateRoleModule(String roleId, String[] moduleIds) {

        roleService.updateRoleModule(roleId,moduleIds);

        return "redirect:/system/role/roleModule.do?roleId=" + roleId;
    }
}
